﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using TodoList.Converter;
using TodoList.ViewModel;
using TodoList.Mode;
using TodoList.Common;
using TodoList.View;
using FirstFloor.ModernUI.Windows.Controls;

namespace TodoList.Command
{
    class EditSave : ICommand
    {
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            TodoBind todoMerget = parameter as TodoBind;
            TodoVM vm = todoMerget.TodoViewModel;
            EditControl ctrl = todoMerget.ObjectType as EditControl;
            List<Todo> todoList = Utils.GetTodoList(vm.FileName);
            Todo toRemove = todoList.Where(item => item.Id.Equals(vm.Todo.Id)).FirstOrDefault();
            todoList.Remove(toRemove);           
            todoList.Add(vm.Todo);
            Utils.SaveTodoItem(vm.FileName, todoList);
            //ReLoad the saved Item
            todoList = Utils.GetTodoList(vm.FileName);
            vm.TodoList = todoList;
            ModernDialog dlog = ctrl.Tag as ModernDialog;
            dlog.Close();
            Console.WriteLine("Item Edit");
        }
        #endregion
    }
}
